package som.simon.reactor.sinks;

import reactor.core.publisher.Flux;
import som.simon.reactor.SimonUtil;

import java.time.Duration;

public class CacheMain {

    public static void main(String[] args) {

        Flux<String> flux = Flux.range(1, 10)
                .delayElements(Duration.ofSeconds(1))
                .cache(3)
                .map(SimonUtil::format);

        flux.subscribe(
                v -> System.out.println("v1 = " + v),
                System.out::println,
                () -> System.out.println("game over....1")
        );


        new Thread(() -> {
            SimonUtil.sleep(5);
            flux.subscribe(
                    v -> System.out.println("v2 = " + v),
                    System.out::println,
                    () -> System.out.println("game over....2")
            );
        }).start();

        SimonUtil.pause();

    }


    /**
     * v1 = 0000001
     * v1 = 0000002
     * v1 = 0000003
     * v1 = 0000004
     * TODO v2 = 0000002
     * TODO v2 = 0000003
     * TODO v2 = 0000004
     * v1 = 0000005
     * v2 = 0000005
     * v1 = 0000006
     * v2 = 0000006
     * v1 = 0000007
     * v2 = 0000007
     * v1 = 0000008
     * v2 = 0000008
     * v1 = 0000009
     * v2 = 0000009
     * v1 = 0000010
     * v2 = 0000010
     * game over....1
     * game over....2
     */

}
